<!-- 小程序列表 -->
<template>
	<div class="app-container">
		<div class="wx-user-box">
			<div class="app-title">微信小程序列表</div>
			<el-row :gutter="20">
				<el-col :span="6">
					<el-card class="box-card">
						<div>当月提审限额：{{ quota.limit || 0 }}</div>
					</el-card>
				</el-col>
				<el-col :span="6">
					<el-card class="box-card">
						<div>剩余提审限额：{{ quota.rest || 0 }}</div>
					</el-card>
				</el-col>
				<el-col :span="6">
					<el-card class="box-card">
						<div>当月加急次数：{{ quota.speedup_rest || 0 }}</div>
					</el-card>
				</el-col>
				<el-col :span="6">
					<el-card class="box-card">
						<div>剩余加急次数：{{ quota.speedup_limit || 0 }}</div>
					</el-card>
				</el-col>
			</el-row>

			<el-form style="margin-top: 20px;" :inline="true" :model="formInline" class="demo-form-inline">
				<el-form-item label="">
					<el-input v-model="formInline.name" style="width: 300px;" clearable placeholder="请输入小程序名称" @change="sousuo()"></el-input>
				</el-form-item>
				<el-form-item><el-button type="primary" @click="sousuo()" icon="el-icon-search">查询/筛选</el-button></el-form-item>
				<el-form-item><el-button type="success" @click="batchUpgrade()">一键升级</el-button></el-form-item>
			</el-form>

			<el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" border>
				<el-table-column type="selection" width="55"></el-table-column>
				<el-table-column prop="appid" label="APPID"></el-table-column>
				<el-table-column prop="address" label="logo" width="80px">
					<template slot-scope="scope">
						<img :src="scope.row.head_img" style="width: 50px;height: 50px;" />
					</template>
				</el-table-column>
				<el-table-column prop="nick_name" label="名称"></el-table-column>
				<el-table-column prop="template_version" label="当前版本"></el-table-column>
				<el-table-column prop="audit_id" label="审核ID"></el-table-column>
				<el-table-column prop="audit_version" label="审核版本"></el-table-column>
				<el-table-column prop="wy_name" label="审核状态">
					<template slot-scope="scope">
						<el-tag v-if="scope.row.audit_state == 2">审核中</el-tag>
						<el-tag v-else-if="scope.row.audit_state == 0" type="info">无新版本审核</el-tag>
						<el-tag v-else-if="scope.row.audit_state == 1" type="warning">待审核</el-tag>
						<el-tag v-else-if="scope.row.audit_state == 3" type="danger">审核失败</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="体检二维码" width="100px">
					<template slot-scope="scope">
						<el-image v-if="scope.row.qrcode_url" style="width: 50px; height: 50px" :src="scope.row.qrcode_url" :preview-src-list="[scope.row.qrcode_url]"></el-image>
					</template>
				</el-table-column>
				<el-table-column prop="updatetime" label="最后更新时间"></el-table-column>
				<el-table-column label="操作" width="100px">
					<template slot-scope="scope">
						<el-dropdown v-if="latest_version.version != scope.row.template_version ">
							<el-button size="mini">
								操作
								<i class="el-icon-arrow-down el-icon--right"></i>
							</el-button>
							<el-dropdown-menu slot="dropdown">
								<el-dropdown-item @click.native="upgrade(scope.row)" v-if="latest_version.version != scope.row.audit_version && scope.row.audit_state != 2">新版本上传</el-dropdown-item>
								<el-dropdown-item @click.native="audit(scope.row)" v-if="scope.row.audit_state == 1 || scope.row.audit_state == 3">提交审核</el-dropdown-item>
								<el-dropdown-item @click.native="updAuditState(scope.row.appid)" v-if="scope.row.audit_state == 2">查看审核状态</el-dropdown-item>
								<el-dropdown-item @click.native="un_audit(scope.row.appid)" v-if="scope.row.audit_state == 2">撤销审核</el-dropdown-item>
								<el-dropdown-item @click.native="speedupaudit(scope.row.appid)" v-if="scope.row.audit_state == 2">加急审核</el-dropdown-item>
								<el-dropdown-item @click.native="getprivacysetting(scope.row.appid)" >查看隐私声明</el-dropdown-item>
								<el-dropdown-item @click.native="setprivacysetting(scope.row.appid)" >设置隐私声明</el-dropdown-item>
							</el-dropdown-menu>
						</el-dropdown>
					</template>
				</el-table-column>
			</el-table>
			<div class="el-pagination">
				<el-pagination
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="page"
					:page-sizes="[10, 100, 200, 300, 400]"
					:page-size="size"
					layout="total, sizes, prev, pager, next, jumper"
					:total="total"
				></el-pagination>
			</div>
		</div>
	</div>
</template>

<script>
import Api from '@/api/pages/xcx/index.js';
export default {
	name: 'xcx_list',
	data() {
		return {
			formInline: {
				name: ''
			},
			page: 1,
			size: 10,
			total: 0,
			tableData: [],
			eleList: [],
			latest_version: {
				version: ''
			},
			quota: ''
		};
	},
	mounted() {
		this.getData();
		this.queryquota();
	},
	methods: {
		getData() {
			let data = {
				pageNumber: this.page,
				pageSize: this.size
			};
			if (this.formInline.name) {
				data['nick_name'] = this.formInline.name.trim();
			}
			Api.query(data).then(res => {
				if (res.state) {
					this.tableData = res.data.dataPage.list;
					this.total = res.data.dataPage.totalRow;
					this.latest_version = res.data.latest_version || {version:null};
				} else {
					this.$message(res.msg);
				}
			});
		},
		// 获取提交次数
		queryquota() {
			Api.queryquota().then(res => {
				if (res.state) {
					this.quota = res.data;
				} else {
					this.$message(res.msg);
				}
			});
		},

		// 一键升级
		batchUpgrade() {
			if(!this.eleList.length) {
				this.$message({
					  showClose: true,
					  message: '请选择需要升级的小程序',
					  type: 'warning'
				});
				return
			}
			this.$confirm('您确定将已勾选的小程序升级到最新版本并提交审核吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					let ids = []
					this.eleList.forEach(item => {
						ids.push(item.id)
					})
					Api.batch_upgrade({
						appids:ids.join()
					}).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		// 新代码上传
		upgrade(item) {
			let data = {
				appid:item.appid,
				template_id:this.latest_version.source_id,
			}
			this.$confirm('版本号：'+this.latest_version.version+'，版本描述：'+this.latest_version.desc, '是否升级到最新版本？', {
				confirmButtonText: '升级',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.upgrade(data).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		// 撤销审核
		un_audit(appid) {
			this.$confirm('您确定要撤销审核吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.un_audit({
						appid:appid
					}).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		// 加急审核
		speedupaudit(appid) {
			this.$confirm('加急后的小程序预计2-12小时内审核完成，确定要加急审核吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.speedupaudit({
						appid:appid
					}).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		// 查看当前审核状态
		updAuditState(appid) {
			this.$confirm('手动查询审核状态，如状态已变更，将自动更新?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.updAuditState({
						appid:appid
					}).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		// 提交审核
		audit(item) {
			let data = {
						appid:item.appid,
						source_id:item.source_id,
					}
					console.log(data,item)
			this.$confirm('您要提交审核吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.audit(data).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		
			// 设置隐私协议
		setprivacysetting(appid) {
			this.$confirm('确定要设置隐私声明吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.setprivacysetting({
						appid:appid
					}).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		// 设置隐私协议
		getprivacysetting(appid) {
			this.$confirm('确定要查看隐私声明吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					Api.getprivacysetting({
						appid:appid
					}).then(res => {
						if (res.state) {
							this.$message({
							  showClose: true,
							  message: res.msg,
							  type: 'success'
							});
						} else {
							this.$message(res.msg);
						}
						this.getData();
					});
				})
				.catch(() => {});
		},
		sousuo() {
			this.page = 1;
			this.getData();
		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
			this.size = val;
			this.getData();
		},
		handleCurrentChange(val) {
			console.log(`当前页: ${val}`);
			this.page = val;
			this.getData();
		},
		closeReplyList() {
			this.dialogVisible1 = false;
		},
		handleSelectionChange(e) {
			this.eleList = e;
		}
	}
};
</script>
<style lang="scss" scoped></style>

<style lang="scss">
.dialogVideo {
	.el-dialog__body {
		padding: 20px;
	}
}
.xcx .el-card__header {
	padding: 10px 20px !important;
}
</style>
